package com.addcn.caruimodule.text;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import androidx.appcompat.widget.AppCompatTextView;

import com.bumptech.glide.Glide;

import java.util.List;


/**
 * 自定義TextView的動畫
 */
public class UpDownTextView extends AppCompatTextView implements Animator.AnimatorListener{

    private static final String TAG = "UpDownTextView";

    private static final int ANIMATION_DURATION = 200;
    private float height;
    private AnimatorSet mAnimatorStartSet;
    private AnimatorSet mAnimatorEndSet;
    private String mText;
    private List<UpDownBean> list;
    private Handler handler;
    private int time = 3000;
    private int index = 0;
    private int currentIndex = 0;
    private ImageView imageView;

    public UpDownTextView(Context context) {
        super(context);
    }

    public UpDownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 此方法就是獲取view的高度
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        height = getHeight();
    }

    /**
     * --- 初始化向上脫離螢幕的動畫效果 ---
     */
    private void initStartAnimation() {
        ObjectAnimator translate = ObjectAnimator.ofFloat(this, "translationY", 0, -height);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 1f, 0f);
        mAnimatorStartSet = new AnimatorSet();
        mAnimatorStartSet.play(translate).with(alpha);
        mAnimatorStartSet.setDuration(ANIMATION_DURATION);
        mAnimatorStartSet.addListener(this);
    }

    /**
     * --- 初始化從螢幕下面向上的動畫效果 ---
     */
    private void initEndAnimation() {
        //translationY 屬性肯定要歸為0，不然顯示的內容就有偏移了；alpha 也是一樣
        ObjectAnimator translate = ObjectAnimator.ofFloat(this, "translationY", height, 0);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 0f, 1f);
        mAnimatorEndSet = new AnimatorSet();
        mAnimatorEndSet.play(translate).with(alpha);
        mAnimatorEndSet.setDuration(ANIMATION_DURATION);
        mAnimatorStartSet.addListener(this);
    }

    public void start(){
        if (list != null){
            if (handler == null){
                handler = new Handler();
            }
            if (list.size() >0){
                handler.postDelayed(runnable,time);
            }
        }
    }

    public void stop(){
        if (handler != null && runnable != null){
            handler.removeCallbacks(runnable);
        }
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, time);
            index++;
            if (index > list.size()) {
                index = 0;
            }
            if (index < list.size() && list.size() >= 0){
                setText(list.get(index).getContent());
                if (imageView != null){
                    //BitmapUtil.displayImage(list.get(index).getIconUrl(),imageView,getContext());
                    Glide.with(getContext()).load(list.get(index).getImgPath()).into(imageView);
                }
            }
            currentIndex = index;
        }
    };



    /**
     * --- 設定內容 ---
     **/
    private void setText(String text) {
        if (TextUtils.isEmpty(text)) {
            Log.e(TAG, "--- 請確保text不為空 ---");
            return;
        }
        mText = text;
        if (null == mAnimatorStartSet) {
            initStartAnimation();
        }
        mAnimatorStartSet.start();
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public void setList(List<UpDownBean> list){
        this.list = list;
    }

    public ImageView getImageView() {
        return imageView;
    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
        if (currentIndex < list.size() && list.size() >= 0){
            setText(list.get(currentIndex).getContent());
            if (imageView != null){
               // TCBitmapUtil.displayImage(list.get(currentIndex).getIconUrl(),imageView,getContext());
                Glide.with(getContext()).load(list.get(currentIndex).getImgPath()).into(imageView);
            }
        }
    }

    @Override
    public void onAnimationStart(Animator animator) {

    }

    @Override
    public void onAnimationEnd(Animator animator) {
        super.setText(mText);
        if (null == mAnimatorEndSet) {
            initEndAnimation();
        }
        mAnimatorEndSet.start();
    }

    @Override
    public void onAnimationCancel(Animator animator) {

    }

    @Override
    public void onAnimationRepeat(Animator animator) {

    }






}
